package com.hkbigdata.reallywar;

import com.hkbigdata.bean.UserBehavior;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;
import scala.Int;

import java.util.HashSet;

/**
 * @author liuanbo
 * @creat 2024-04-23-17:55
 * @see 2194550857@qq.com
 */
public class Flink02_Project_UV {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment emv = StreamExecutionEnvironment.getExecutionEnvironment();

        emv.setParallelism(1);

        emv.readTextFile("input/UserBehavior.csv")
                .flatMap((String data, Collector<Tuple2<String, Long>> out) -> {
                    String[] split = data.split(",");
                    UserBehavior userBehavior = new UserBehavior(
                            Long.valueOf(split[0]),
                            Long.valueOf(split[1]),
                            Integer.valueOf(split[2]),
                            split[3],
                            System.currentTimeMillis()
                    );

                    if ("pv".equals(userBehavior.getBehavior())) {
                        out.collect(Tuple2.of("uv", userBehavior.getUserId()));
                    }
                }).returns(Types.TUPLE(Types.STRING, Types.LONG))
                .keyBy(data -> data.f0)
                .process(new KeyedProcessFunction<String, Tuple2<String, Long>, Integer>() {
                    private HashSet<Long> userids = new HashSet<>();

                    @Override
                    public void processElement(Tuple2<String, Long> value, Context ctx, Collector<Integer> out) throws Exception {
                        userids.add(value.f1);
                        out.collect(userids.size());
                    }
                })
                .print();

        emv.execute();
    }
}
